// pages/index/Share/ShareHotMoney.js
var app = getApp();
const http = require("../../../utils/util.js");
var Crypto = require('../../../utils/cryptojs/cryptojs.js').Crypto;
const recorderManager = wx.getRecorderManager();
const innerAudioContext = wx.createInnerAudioContext();
var tempFilePath;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo:[],
    options:[],
    isShowShare:false,  //判断显示的页面， false 为 转发按钮页面，

    isLingqu:true,
    windowWidth:'',
    windowHeight:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that  = this;
    console.log(options)
    that.setData({
      userInfo: app.globalData.userInfo,
      options:options,
    })
    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          windowWidth:res.windowWidth,
          windowHeight:res.windowHeight,
        })
      },
    })
    that.lingHongBao();
  },

  // 判断红包状态
lingHongBao:function(){
  var that = this;
  wx.getStorage({
    key: 'token',
    success: function(res) {
      var token = res.data;
      var timestamp = app.timestamp();
      var url = app.globalData.localApi + "/redpack/uploadVoice";
      wx.request({
        url: url,
        method:"POST",
        data:{
          token:token,
          timestamp:timestamp,
          sign: Crypto.MD5(app.sign(url,token,timestamp)),
          redpackId:"701a1b2af34f431a8d4d6c8151d29411",
        },
        success:function(res){
          console.log("uploadVoice请求成功")
          console.log(res)
        },
        fail:function(res){
          console.log(res)
        }
      })

    },
  })
},

  toshareChat:function(){
    
  },
  toTixian:function(){
    wx.navigateTo({
      url: '../../tixian/tixian',
    })
  },
  toindex:function(){
    wx.navigateTo({
      url: '../index',
    })
  },
  toshare:function(){
    var that = this;
    console.log("去转发")
   that.setData({
     isShowShare:!that.data.isShowShare,
   })
  },

  // 获取语音
  startVoice: function () {
    var that = this;
    console.log("开始录音")
    app.getVoice();
  },
  endstartVoice: function () {
    var that = this;
    recorderManager.stop();
    recorderManager.onStop((res) => {
      console.log('停止录音', res.tempFilePath)
      that.uploadVoice(res.tempFilePath);
    })
  },
  // 上传语音：
  uploadVoice: function (tempFilePath) {
    var that = this;
    console.log("程序进入上传语音");
    // var tempFilePath = that.tempFilePath
    console.log(tempFilePath);
    wx.getStorage({
      key: 'token',
      success: function (res) {
        const token = res.data;
        const url = app.globalData.localApi + "/redpack/receiveVoice";
        console.log("上传语音url" + url);
        console.log("上传语音filePath" + tempFilePath);

        wx.uploadFile({
          url: url,
          filePath: tempFilePath,
          name: "file",
          formData: {
            'user': 'test'
          },
          header: { "Content-Type": "multipart/form-data" },
          success: function (res) {
            console.log("请求成功")
            console.log(res)
            that.setData({
              voiceResult: true,
            })
          },
          fail: function (res) {
            console.log(res)
            console.log("上传失败")
            app.showInfo("网络开小差了,语音没有上传成功")

            that.setData({
              voiceResult: false,
            })
          }
        })
      },
    })
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */

    onShareAppMessage: function (res) {
      var that = this;
      console.log("开始执行转发")
      return {
        title: "小程序红包",
        path: "/pages/index/Share/share?Money=" + that.data.options.Money
        + "&Number=" + that.data.options.Number
        + "&kouling=" + that.data.options.kouling
        + "&houBaoStyle=" + that.data.options.houBaoStyle
        + "&style=1"
        + "&orderno=" + that.data.options.orderno,
        success: function (res) {
          app.showInfo("转发成功")
          console.log(res)
          that.setData({
            isShowShare:!that.data.isShowShare
          })
        },
        fail: function (res) {
          app.showInfo("转发失败")
          console.log(res)
        }

      }
    }
  
})